<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" v-show="showSearch" label-width="68px" class="form-flex">
      <el-row :gutter="20">
        <el-col :span="6" :xs="24">
          <el-form-item label="消息标题" prop="title">
            <el-input
              v-model="queryParams.title"
              placeholder="请输入消息标题"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="发布人" prop="senderName">
            <el-input
              v-model="queryParams.senderName"
              placeholder="请输入发布人"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item label="消息类型" prop="messageType">
            <el-select v-model="queryParams.messageType" placeholder="请选择消息类型" clearable size="small">
              <el-option
                v-for="item in dict.message_type"
                :key="item.dictValue"
                :label="item.dictLabel"
                :value="item.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6" :xs="24">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-refresh" size="mini" @click="handleUpdateNoRead">全部标注为已读
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="messageList" >
      <el-table-column label="消息标题" align="center" prop="title"/>
      <el-table-column label="发布者" align="center" prop="senderName"/>
      <el-table-column label="发布时间" align="center" prop="senderTime" width="180"/>
      <el-table-column label="消息类型" align="center" prop="messageType">
        <template slot-scope="scope">
          {{ dict.label.message_type[scope.row.messageType] }}
        </template>
      </el-table-column>
      <el-table-column label="阅读状态" align="center" prop="haveRead" >
        <template slot-scope="scope">
          {{ dict.label.have_read[scope.row.haveRead] }}
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleView(scope.row,scope.$index)"
          >查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <notice-detail ref="noticeDetailRef" />


  </div>
</template>

<script>

  import {listMixin} from "@/utils/mixins/listMixin";
  import { myNoticeList,updateNoRead,updateHaveRead } from "@/api/notice/noticeSiteMessage";
  import NoticeDetail from "@/layout/components/notice-icon/detail"
  export default {
    name: "myMessage",
    mixins: [listMixin],
    components:{ NoticeDetail },
    dicts: ['message_type', 'have_read'],
    data() {
      return {
        // 站内消息表格数据
        messageList: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          title: null,
          content: null,
          haveRead: null,
          senderName: null,
          messageType: null
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询站内消息列表 */
      getList() {
        this.loading = true;
        //只看自己发布的消息
        this.queryParams.receiveId = this.$store.state.user.name || -1
        myNoticeList(this.queryParams).then(response => {
          this.messageList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      handleUpdateNoRead(){
        updateNoRead(this.$store.state.user.name || -1).then(res=>{
          this.getList()
          this.msgSuccess("更新成功")
        })
      },
      handleView(row,index){
        this.$refs.noticeDetailRef.show({item: row})
        if(row.haveRead == '10'){
          this.messageList[index].haveRead = '20'
          updateHaveRead(row.id).then(res=>{
            this.$store.dispatch('ReadCount')
          })
        }
      }
    }
  };
</script>
